package com.trkj.wiki.controller;

import com.alibaba.fastjson.JSONObject;
import com.trkj.wiki.req.UserLoginReq;
import com.trkj.wiki.req.UserQueryReq;
import com.trkj.wiki.req.UserResetPasswordReq;
import com.trkj.wiki.req.UserSaveReq;
import com.trkj.wiki.resp.CommonResp;
import com.trkj.wiki.resp.PageResp;
import com.trkj.wiki.resp.UserLoginResp;
import com.trkj.wiki.resp.UserQueryResp;
import com.trkj.wiki.service.UserService;
import com.trkj.wiki.util.SnowFlake;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping("/user")
@Log4j2
public class UserController {
    @Autowired
    private UserService userService;

    @Resource
    private SnowFlake snowFlake;

    @Resource
    private RedisTemplate redisTemplate;


    @GetMapping("/list")
    public CommonResp list(@Valid UserQueryReq req){
        CommonResp<PageResp<UserQueryResp>> resp = new CommonResp<>();
        PageResp<UserQueryResp> list = userService.list(req);
        resp.setContent(list);
        return resp;
    }

    @PostMapping("/save")
    public CommonResp save(@Valid @RequestBody UserSaveReq req){
        req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes()));
        System.out.println("UserSaveReq=>"+req);
        CommonResp resp = new CommonResp<>();
        userService.save(req);
        return resp;
    }


    @DeleteMapping("/delete/{id}")
    public CommonResp delete(@PathVariable("id") long id){
        System.out.println("id=>"+id);
        CommonResp resp = new CommonResp<>();
        userService.delete(id);
        return resp;
    }


    @PostMapping("/reset-password")
    public CommonResp resetPassword(@Valid @RequestBody UserResetPasswordReq req){
        req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes()));
        System.out.println("UserSaveReq=>"+req);
        CommonResp resp = new CommonResp<>();
        userService.resetPasword(req);
        return resp;
    }
    @PostMapping("/login")
    public CommonResp login(@Valid @RequestBody UserLoginReq req){
        req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes()));
        System.out.println("UserSaveReq=>"+req);
        CommonResp<UserLoginResp> resp = new CommonResp<>();
        UserLoginResp userLoginResp=userService.login(req);

        //生成单点登录token，并放入redis中
        Long token =snowFlake.nextId();
        log.info("生成单点登录token {}，并放入redis中",token);
        userLoginResp.setToken(token.toString());
        redisTemplate.opsForValue().set(token.toString(), JSONObject.toJSONString(userLoginResp),3600*24, TimeUnit.SECONDS);
        resp.setContent(userLoginResp);
        return resp;
    }


    @GetMapping("/logout/{token}")
    public CommonResp logout(@PathVariable String token){
        System.out.println("token=>"+token);
        CommonResp resp = new CommonResp<>();
        redisTemplate.delete(token);
        log.info("从redis中删除token {}",token);
        return resp;
    }
}
